package com.ruoyi.web.job;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.TblInterfaces;
import com.ruoyi.system.mapper.TblInterfacesMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Configuration
@EnableScheduling
public class EveryDayCheckTask {
    private static final String FS_NOTIFY_MSG = "https://sany-one-prod.sany.com.cn/messagesender/fs/v1/sendFsTxtMsg";
	private static Logger logger = LogManager.getLogger(EveryDayCheckTask.class);

	@Autowired
	private TblInterfacesMapper tblInterfacesMapper;

	//秒 分 时 日 月 周 年
//	@Scheduled(cron="0 */5 * * * ?")
    public void taskEndFissionWanted(){
    	//每天上午8点执行，报告成功通知
        //0 */5 * * * ?
        logger.info("每天8点检查接口... ");
//		List<TblInterfaces> tblInterfacesList = tblInterfacesMapper.selectTblInterfacesList(null);
//		if (tblInterfacesList == null || tblInterfacesList.isEmpty()){
//		    logger.info("5分钟检查接口没有接口数据，返回");
//		    return;
//        }
//        //按项目分类
//        Map<String, List<TblInterfaces>> maps = new HashMap<>();
//        for (TblInterfaces item:tblInterfacesList) {
//            if (item == null){
//                continue;
//            }
//            if (StringUtils.isEmpty(item.getProject())){
//                continue;
//            }
//            if (maps.containsKey(item.getProject())){
//                List<TblInterfaces> list = maps.get(item.getProject());
//                list.add(item);
//            }else{
//                List<TblInterfaces> list = new ArrayList<>();
//                list.add(item);
//                maps.put(item.getProject(), list);
//            }
//        }
//        for (String key:maps.keySet()) {
//
//        }
//        boolean allSuccess = false;
//        for (TblInterfaces item:tblInterfacesList) {
//		    if (item == null){
//                logger.info("对象内容为空，跳过");
//		        continue;
//            }
//            if (item.getMethod() == null){
//                logger.info("请求方法为空，跳过");
//		        continue;
//            }
//            if (StringUtils.isEmpty(item.getSuccessStr())){
//                logger.info("没有成功字符串，跳过");
//                continue;
//            }
//
//            logger.info("开始请求[{}]", item);
//            if (StringUtils.equals(item.getMethod().toLowerCase(), "get")){
//                StringBuilder sb = new StringBuilder(item.getUrl());
//                if (StringUtils.isNotEmpty(item.getUrlParams())){
//                    sb.append(item.getUrlParams());
//                }
//                String result = HttpUtils.sendGet(sb.toString());
//                if (StringUtils.isEmpty(result) || !result.contains(item.getSuccessStr())){
//                    allSuccess = false;
//                    break;
//                }
//            }
//        }
//        if (allSuccess){
//
//        }
//        System.out.println(tblInterfacesList);
    }

    public static void sendFsMsg(TblInterfaces item, String result){
        FsMsgSendRequestModel fsMsgModel = new FsMsgSendRequestModel();
        fsMsgModel.setAppId("cli_a1bfdb560bb8d076");
        fsMsgModel.setAppSecret("45VTOq7EsUFhCZf1yrcISdIv3K4ZmekT");

        String[] fsUserNames = item.getFsNotifyNames().split(",");
        for (String userName:fsUserNames) {
            fsMsgModel.setUserName(userName);
            fsMsgModel.setContent("项目: " + item.getProject() + "\n"
                    + "接口: " + item.getUrl()
                    + "请求失败\n"
                    + "返回内容: " + result);

            try {
                HttpUtils.testPost(FS_NOTIFY_MSG, fsMsgModel);
            } catch (Exception e) {
                logger.error("", e);
            }
        }
    }

}